package ch.ethz.eai.handler;

import java.util.Iterator;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNode;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPHeader;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.handlers.AbstractHandler;

public class AuthenticationHandler extends AbstractHandler {

	@Override
	public InvocationResponse invoke(MessageContext mc) throws AxisFault {
		SOAPEnvelope envelope = mc.getEnvelope();
		SOAPHeader header = envelope.getHeader();
		if (header!=null)
		{
			Iterator i = header.getChildElements();
			while (i.hasNext()) {
				OMElement el = (OMElement) i.next();
				if (el.getLocalName().equals("userToken")) {
					Iterator it = el.getChildElements();
					while (it.hasNext()) {
						OMElement elem = (OMElement) it.next();
						if (elem.getLocalName().equals("secretToken"))
							return InvocationResponse.CONTINUE;
						else
							return InvocationResponse.ABORT;
					}
				}
			}
			return InvocationResponse.ABORT;
		}
		else
			return InvocationResponse.CONTINUE;
	}

}
